import {
  createInbizSchema,
  createBaseSchema,
  createComponentStyleSchema,
  createDecoratorStyleSchema,
  createBizStyleSchema,
  createCollapseToBaseSchema,
  getDefalutDecoratorProps,
  createControllerSchema
} from '@inbiz/setters';
import IndexSetting from './components/indexSetting'; // 索引库配置
import { createBehavior, createResource } from '@inbiz/core';
import { SearchEngine as LocaleSearchEngine } from './locale';
import { getPlatform } from '@inbiz/utils';

/**
 * 1.属性的默认值:defaultProps
 * 2.属性的json数据
 * */

export const defaultProps = {
  // largePanelView
  inIcon: { type: 'icon-inbiz_basic-search' },
  inTitle: {
    value: 'SearchEngine.search',
    status: true,
  },
  searchIcon: { type: 'icon-inbiz_basic-search' },
  searchText: {
    value: 'SearchEngine.search',
    status: true,
  },
  buttonText: {
    value: 'SearchEngine.search',
    status: true,
  },
  align: 'center',
  placeholderWeb: 'SearchEngine.enterSearchContent',
  endSearchIcon: {
    type: '',
  },
  resultSearch: {
    value: 'SearchEngine.resultSearch',
    status: true,
  },
  viewType: 'largePanelView', // 视图类型:['内嵌视图', '大面板视图'] 默认是内嵌视图
  wapviewType: 'inlineView',
  sort: '',
  searchResultType: 'page',
  order: '',
  className: '',
  icon: {
    type: 'icon-inbiz_basic-search',
    size: '32px',
    color: 'rgb(102, 102, 102)',
  },
  style:
    getPlatform() === 'web'
      ? {
        padding: '15px 20px 15px 20px',
      }
      : undefined,
  hotKeywordsSwitch: true, // 是否显示热门搜索
  hotKeywordsPosition: 'inside', // 热门搜索显示位置
  hotKeywordsNumber: 8, // 热门搜索数量
  detection: false, // 检索条件
  associativeWords: true, // 联想词
  indexRange: true, // 索引范围
  pullWidth: 100, // 面板宽度（px）
  searchResultPageType: '_blank',
  placeholder: 'SearchEngine.enterSearchContent',
  // 索引库配置数据源
  tabsConfig: [
    {
      guid: 'all_202101031218', // 编号
      title: 'SearchEngine.whole', // 索引库名称
      isAll: true, // 全部搜索
      isShow: false, // 是否显示
      isEdit: true, // 是否可以编辑
      isDelete: false, // 是否可以删除
      searchMode: false, // 打开搜索方式(模糊搜索和精确搜索)
      indexLibraryName: '202101031218',
    },
    {
      guid: 'file_202101031117', // 编号
      title: 'SearchEngine.file',
      isFile: true,
      isShow: true, // 是否显示
      isEdit: true, // 是否可以编辑
      isDelete: false, // 是否可以删除
      advancedSearch: false, // 开启高级搜索
      customRange: ['allFolder', 'curfolder'], // 可选全库或者当前文件夹
      searchFolderName: 'SearchEngine.enterpriseContentLibrary', // DMS搜索文件夹名
      searchMode: false, // 打开搜索方式(模糊搜索和精确搜索)
      advancedSearchStyle: 'style2', // 高级搜索样式
      quikSearchTemplate: 'file.jsx',
      filepath: InbizConfig.PUBLIC_PATH + '/template_search/file.jsx',
      indexLibraries: [
        {
          "IndexName": "file",
          "IndexCode": "0536ce92cbca4e359d1d448349fa2260",
          "IndexDesc": "ECM文件索引",
          "IndexType": 2,
          "Id": 1,
          "configs": [
            {
              "title": "file",
              "defaultSearchCriteria": "*",

              "sortField": [
                {
                  "sort": "desc",
                  "enable": true,
                  "IndexFieldDesc": "SearchEngine.matchingDegree",
                  "IndexFieldName": "",
                  "default": true
                }
              ]
            }
          ]
        }
      ]
    },
    {
      guid: 'folder_202101031118', // 编号
      title: 'SearchEngine.folder',
      isFolder: true,
      isShow: true, // 是否显示
      isEdit: true, // 是否可以编辑
      isDelete: false, // 是否可以删除
      advancedSearch: false, // 开启高级搜索
      customRange: ['allFolder', 'curfolder'], // 可选全库或者当前文件夹
      searchFolderName: 'SearchEngine.enterpriseContentLibrary', // DMS搜索文件夹名
      searchMode: false, // 打开搜索方式(模糊搜索和精确搜索)
      advancedSearchStyle: 'style2', // 高级搜索样式
      quikSearchTemplate: 'folder.jsx',
      filepath: InbizConfig.PUBLIC_PATH + '/template_search/folder.jsx',
      indexLibraries: [
        {
          "IndexName": "folder",
          "IndexCode": "dfb12a6ac68147169a9c3079df562ee6",
          "IndexDesc": "ECM文件夹索引",
          "IndexType": 2,
          "Id": 6,
          "configs": [
            {
              "title": "folder",
              "defaultSearchCriteria": "*",
              "sortField": [
                {
                  "sort": "desc",
                  "enable": true,
                  "IndexFieldDesc": "SearchEngine.matchingDegree",
                  "IndexFieldName": "",
                  "default": true
                }
              ]
            }
          ]
        }
      ]
    },
  ],
  historySearch: true,
  quikSearch: true,
  synonymSearch: false,
  fixedWidth: '420px'
};
// Shema
export const propsSchema = createInbizSchema({
  attribute: {
    ...createCollapseToBaseSchema('group_viewType', {
      viewType: { //视图类型
        type: 'string',
        enum: ['inlineView', 'largePanelView', 'smallPanelView'],
        'x-decorator': 'FormItem',
        'x-component': 'Select',
        'x-component-props': {
          defaultValue: 'inlineView',
        },
        'x-reactions': {
          target: 'form.attribute.base.title',
          fulfill: {
            state: {
              hidden: "{{$self.value == 'inlineView'}}",
            },
          },
        },
        platform: 'web',
      },
      wapviewType: { //视图类型
        type: 'string',
        enum: ['inlineView', 'largePanelView'],
        'x-decorator': 'FormItem',
        'x-component': 'Select',
        'x-component-props': {
          defaultValue: 'inlineView',
        },
        'x-reactions': {
          target: 'form.attribute.base.title',
          fulfill: {
            state: {
              hidden: "{{$self.value == 'inlineView'}}",
            },
          },
        },
        platform: 'wap',
      },
    }),
    base: {
      type: 'void',
      'x-component': 'CollapseItem',
      properties: {
        'x-display': {
          type: 'string',
          enum: ['visible', 'hidden'],
          'x-decorator': 'FormItem',
          'x-component': 'Select',
          'x-component-props': {
            defaultValue: 'visible',
          },
        },
        'x-component-props': {
          type: 'object',
          properties: {
            inIcon: { // 入口图标
              default: { type: 'icon-inbiz_basic-search' },
              type: 'object',
              'x-decorator': 'FormItem',
              'x-component': 'IconPicker',
              'x-component-props': {
                showIconSize: false,
                showColorPicker: false,
              },
              'x-reactions': {
                dependencies: ['.viewType'],
                fulfill: {
                  schema: {
                    'x-visible': "{{$deps[0] == 'largePanelView'}}",
                  },
                },
              },
              platform: 'web',
            },

            inTitle: { // 入口文字
              type: 'object',
              'x-decorator': 'FormItem',
              'x-component': 'StatusInput',
              platform: 'web',
              'x-reactions': {
                dependencies: ['.viewType'],
                fulfill: {
                  schema: {
                    'x-hidden': "{{$deps[0] !== 'largePanelView'}}",
                  },
                },
              },
            },
            searchIcon: { // 搜索图标
              default: { type: 'icon-inbiz_basic-search' },
              type: 'object',
              'x-decorator': 'FormItem',
              'x-component': 'IconPicker',
              'x-component-props': {
                showIconSize: false,
                showColorPicker: false,
              },
              'x-reactions': {
                dependencies: ['.viewType'],
                fulfill: {
                  schema: {
                    'x-visible': "{{$deps[0] == 'smallPanelView'}}",
                  },
                },
              },
              platform: 'web',
            },
            // searchText: { // 搜索文字
            //   type: 'object',
            //   'x-decorator': 'FormItem',
            //   'x-component': 'StatusInput',
            //   'x-reactions': {
            //     dependencies: ['.viewType'],
            //     fulfill: {
            //       schema: {
            //         'x-visible': "{{$deps[0] == 'smallPanelView'}}",
            //       },
            //     },
            //   },
            //   platform: 'web',
            // },
            buttonIcon: { // 按钮图标
              default: { type: 'icon-inbiz_basic-search' },
              type: 'object',
              'x-decorator': 'FormItem',
              'x-component': 'IconPicker',
              'x-component-props': {
                showIconSize: false,
                showColorPicker: false,
              },
              'x-reactions': {
                dependencies: ['.viewType'],
                fulfill: {
                  schema: {
                    'x-hidden': "{{$deps[0] == 'smallPanelView'}}",
                  },
                },
              },
              platform: 'web',
            },
            buttonText: { // 按钮文字
              type: 'object',
              'x-decorator': 'FormItem',
              'x-component': 'StatusInput',
              'x-reactions': {
                dependencies: ['.viewType'],
                fulfill: {
                  schema: {
                    'x-hidden': "{{$deps[0] == 'smallPanelView'}}",
                  },
                },
              },
              platform: 'web',
            },
            align: { // 对齐方式
              type: 'string',
              enum: ['center', 'start'],
              'x-decorator': 'FormItem',
              'x-component': 'Select',
              'x-component-props': {
                defaultValue: 'center',
              },
              'x-reactions': {
                dependencies: ['form.attribute.group_viewType.x-component-props.viewType'],
                fulfill: {
                  schema: {
                    'x-hidden': "{{$deps[0] !== 'inlineView'}}",
                  },
                },
              },
              platform: 'web',
            },
            placeholderWeb: { // 搜索提示
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'MultilingualInput',
              platform: 'web',
            },
            interactiveMethods: { // 交互方式
              type: 'string',
              enum: ['defaultWidth', 'fixedWidth'],
              'x-decorator': 'FormItem',
              'x-component': 'Select',
              'x-component-props': {
                defaultValue: 'defaultWidth',
              },
              'x-reactions': {
                dependencies: ['.viewType'],
                fulfill: {
                  schema: {
                    'x-hidden': "{{$deps[0] !== 'smallPanelView'}}",
                  },
                },
              },
              platform: 'web',
            },
            fixedWidth: { // 宽度
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'SizeInput',
              'x-component-props': {
                defaultValue: 420,
                include: ['px'],
              },
              'x-reactions': {
                dependencies: ['.interactiveMethods', '.viewType'],
                fulfill: {
                  schema: {
                    'x-hidden': "{{$deps[0] !== 'fixedWidth' || $deps[1] !== 'smallPanelView'}}",
                  },
                },
              },
              platform: 'web',
            },
            searchResultType: { // 绑定方式
              type: 'string',
              enum: ['page', 'component'],
              'x-decorator': 'FormItem',
              'x-component': 'Select',
              'x-component-props': {
                defaultValue: 'page',
              },
            },
            searchResultComponent: { // 关联组件
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'SelectComponent',
              'x-component-props': {
                range: 'all',
                label: 'title',
                filter: (field: { props: { name: string | string[] } }) => {
                  const name = field.props?.name;
                  return name?.includes?.('SearchResultEngine') && !name.includes('.');
                },
              },
              'x-reactions': {
                dependencies: ['.searchResultType'],
                fulfill: {
                  schema: {
                    'x-visible': '{{$deps[0] == "component"}}',
                  },
                },
              },
            },
            searchResultPage: { // 搜索结果页
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'PageSelect',
              'x-reactions': {
                dependencies: ['.searchResultType'],
                fulfill: {
                  schema: {
                    'x-visible': '{{$deps[0] == "page"}}',
                  },
                },
              },
            },
            searchResultPageType: { // 打开方式
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Select',
              enum: ['_self', '_blank'],
              'x-reactions': {
                dependencies: ['.searchResultType'],
                fulfill: {
                  schema: {
                    'x-visible': '{{$deps[0] == "page"}}',
                  },
                },
              },
              platform: 'web',
            },

            placeholder: { // 搜索提示
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'MultilingualInput',
              platform: 'wap',
            },
          },
        },
      },
    },
    ...createCollapseToBaseSchema('indexRange', {
      indexRange: { // 搜索范围
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
        'x-reactions': {
          target: 'x-component-props.pullWidth',
          fulfill: {
            state: {
              hidden: '{{!$self.value}}',
            },
          },
        },
      },
      pullWidth: {
        // 面板宽度（px）
        type: 'number',
        'x-decorator': 'FormItem',
        'x-component': 'NumberPicker',
        platform: 'web',
      },
      rangeType: {
        default: 'popup',
        type: "string",
        'x-decorator': 'FormItem',
        'x-component': 'Select',
        enum: ['pull', 'popup'],
        platform: 'wap',
      },
      tabsConfig: { // 索引库配置
        'x-decorator': 'FormItem',
        'x-component': IndexSetting,
      },
    }),
    ...createCollapseToBaseSchema('hotKeywordsSwitch', {
      hotKeywordsSwitch: { // 热门搜索
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
      },
      hotKeywordsPosition: {
        // 热门搜索显示位置
        type: 'string',
        enum: ['top', 'bottom', 'inside'],
        'x-decorator': 'FormItem',
        'x-component': 'Select',
        'x-reactions': {
          dependencies: ['.viewType'],
          fulfill: {
            state: {
              hidden: "{{$deps[0] == 'smallPanelView'}}",
            },
          },
        },
        platform: 'web',
      },
      hotKeywordsNumber: { // 热词数量
        type: 'number',
        'x-decorator': 'FormItem',
        'x-component': 'NumberPicker',
        'x-component-props': {
          max: 16,
          min: 1,
        },
        'x-reactions': {
          dependencies: ['.hotKeywordsSwitch'],
          fulfill: {
            schema: {
              'x-visible': '{{$deps[0] == true}}',
            },
          },
        },
      },
      // statisticsCycle: { // 统计周期
      //   type: 'string',
      //   'x-decorator': 'FormItem',
      //   'x-component': 'Select',
      //   enum: ['_self', '_blank'],
      //   'x-reactions': {
      //     dependencies: ['.viewType', '.hotKeywordsSwitch'],
      //     fulfill: {
      //       schema: {
      //         'x-visible': '{{$deps[0] != "smallPanelView" || $deps[1] == true}}',
      //       },
      //     },
      //   },
      //   platform: 'web',
      // },
      // statisticsRange: { // 统计范围
      //   type: 'string',
      //   'x-decorator': 'FormItem',
      //   'x-component': 'Select',
      //   enum: ['_self', '_blank'],
      //   'x-reactions': {
      //     dependencies: ['.viewType'],
      //     fulfill: {
      //       schema: {
      //         'x-visible': '{{$deps[0] != "smallPanelView"}}',
      //       },
      //     },
      //   },
      //   platform: 'web',
      // },
    }),
    ...createCollapseToBaseSchema('searchconfiguration', {
      associativeWords: { // 联想搜索
        type: 'boolean',
        default: true,
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
        'x-reactions': {
          dependencies: ['.viewType'],
          fulfill: {
            schema: {
              'x-visible': '{{$deps[0] != "smallPanelView"}}',
            },
          },
        },
        platform: 'web',
      },
      historySearch: { // 历史搜索
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
      },

      quikSearch: { // 快速搜索
        type: 'boolean',
        default: true,
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
        'x-reactions': {
          dependencies: ['.viewType'],
          fulfill: {
            schema: {
              'x-visible': '{{$deps[0] == "smallPanelView"}}',
            },
          },
        },
      },
      synonymSearch: { // 同义词搜索
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
      },
    }),

    // ...createCollapseToBaseSchema('resultSearch', {

    // }),
    resultSearch: {
      type: 'void',
      'x-component': 'CollapseItem',
      'x-reactions': {
        dependencies: ['form.attribute.group_viewType.x-component-props.viewType'],
        fulfill: {
          schema: {
            'x-hidden': '{{$deps[0] == "smallPanelView"}}',
          },
        },
      },
      properties: {
        'x-component-props': {
          type: 'object',
          properties: {
            endSearchIcon: { // 结果中检索图标
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'IconPicker',
              'x-component-props': {
                showIconSize: false,
                showColorPicker: false,
              },
              platform: 'web',
            },
            resultSearch: { // 结果中检索
              type: 'object',
              'x-decorator': 'FormItem',
              'x-component': 'StatusInput',
              platform: 'web',
            },
            detection: { // 检索条件
              type: 'boolean',
              'x-decorator': 'FormItem',
              'x-component': 'Switch',
              platform: 'web',
            },
          }
        }
      },
      platform: 'web',
    },
  },
  style: {
    ...createComponentStyleSchema({
      icon: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'IconPicker',
        'x-component-props': {
          showIconSize: false,
          showColorPicker: false,
        },
        platform: 'wap',
      },
      ...createBizStyleSchema(undefined, getPlatform() === 'web' ? {} : undefined),
    }),
    ...createDecoratorStyleSchema(),
  },
  controller: createControllerSchema({
    events: [
      {
        name: "onSearch",
        label: "触发搜索",
      },
      {
        name: "onBeforeSearch",
        label: "修改搜索数据",
        isConfigEvent: true,
      },
      {
        name: "onBeforeOpenPage",
        label: "添加关联页面url参数",
        isConfigEvent: true,
      },
      {
        name: "onHistoryParams",
        label: "格式化搜索历史请求参数",
        isConfigEvent: true,
      },
      {
        name: "onHistory",
        label: "自定义搜索历史请求",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onHistoryData",
        label: "格式化搜索历史数据",
        isConfigEvent: true,
      },
      {
        name: "onSearchKeyWordParams",
        label: "格式化推荐参数请求参数",
        isConfigEvent: true,
      },
      {
        name: "onSearchKeyWord",
        label: "自定义推荐参数请求",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onSearchKeyWordData",
        label: "格式化推荐参数数据",
        isConfigEvent: true,
      },
      {
        name: "onHotWordParams",
        label: "格式化热门推荐请求参数",
        isConfigEvent: true,
      },
      {
        name: "onHotWord",
        label: "自定义热门推荐请求",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onHotWordData",
        label: "格式化热门推荐数据",
        isConfigEvent: true,
      },
    ]
  })
});

export const Behavior = createBehavior({
  name: 'SearchEngine',
  extends: ['Field'],
  selector: (node) => node.props?.['x-component'] === 'SearchEngine',
  designerLocales: LocaleSearchEngine,
  designerProps: { propsSchema },
});

export const Resource = createResource({
  icon: 'SearchDoor',
  elements: [
    {
      componentName: 'Field',
      props: {
        type: 'sting',
        title: {
          value: 'SearchEngine.search',
          status: true,
        },
        'x-component': 'SearchEngine',
        'x-component-props': defaultProps,
        'x-decorator': 'Container',
        'x-decorator-props': getDefalutDecoratorProps(getPlatform(), { border: false }),
      },
    },
  ],
});
